# -*- coding: utf-8 -*-
# @Time : 2022/7/12 15:07
# @Author : Dftre
# @Email : 3066417822@qq.com
# @File : ergodicLine.py
# @Project : 代码生成器
import re

from .processor import Processor, GeneratorException


class ErgodicLine(Processor):
    def __init__(self, for_dict: dict[str:list]):
        super().__init__(r'-\{(\w+)}')
        self.for_dict = for_dict

    def sub(self, text):
        M = re.match(self.rule, text)
        if M:
            val = re.sub(r'\s', '', M.group(1))
            if val in self.for_dict:
                textList = []
                for i in self.for_dict[val]:
                    textList.append(re.sub(self.rule[1:], str(i), text))
                return ''.join(textList)
            else:
                raise GeneratorException("ErgodicLine", "不存在真值{}".format(val))
        else:
            return text
